package com.atguigu.spzx.manager.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.atguigu.spzx.manager.mapper.*;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductSaveDTO;
import com.atguigu.spzx.model.dto.product.ProductSearchDTO;
import com.atguigu.spzx.model.entity.product.*;
import com.atguigu.spzx.model.vo.product.ProductVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;
    @Resource
    ProductDetailsMapper productDetailsMapper;
    @Resource
    BrandMapper brandMapper;
    @Resource
    CategoryMapper categoryMapper;

    @Resource
    ProductSkuMapper productSkuMapper;
    @Override
    public PageInfo<ProductVO> list(Integer pageNum, Integer pageSize, ProductSearchDTO productSearchDTO) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductVO> list = productMapper.selectByCondition(productSearchDTO);
        return new PageInfo<>(list);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        //一个商品数据会保存到以下三张表中:根据spuId删除三张表中的商品数据
        //product: id 、product_details： id 、product_sku:product_id
        productMapper.deleteById(id);

        productDetailsMapper.deleteById(id);

        productSkuMapper.deleteByProductId(id);

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(ProductSaveDTO productSaveDTO) {
        //productSaveDTO: 携带了需要保存到三张表中的数据
        //1、spu：product
        Product product = new Product();
        BeanUtils.copyProperties(productSaveDTO , product);
        product.setStatus(0);
        product.setAuditStatus(0);
        productMapper.save(product);
        //2、product_details
        //获取product的id值
        Long productId = product.getId();

        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(productId);
        productDetails.setProductId(productId);
        productDetails.setImageUrls(productSaveDTO.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);
        //3、sku:product_sku
        //雪花算法：使用机器码+当前时间戳+毫秒内维护的序号生成
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        List<ProductSku> productSkuList = productSaveDTO.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSku.setProductId(productId);
            //生成商品码
            productSku.setSkuCode(snowflake.nextIdStr());
            productSku.setSkuName(product.getName()+" "+productSku.getSkuSpec());
            productSku.setStatus(0);
            productSku.setSaleNum(0);
        });
        productSkuMapper.saveBatch(productSkuList);

    }

    @Override
    public ProductSaveDTO getById(Long id) {
        //1、查询product
        Product product = productMapper.selectById(id);
        //2、查询product的详情图
        ProductDetails productDetails = productDetailsMapper.selectById(id);
        //3、查询productSku数据
        List<ProductSku> productSkus = productSkuMapper.selectByProductId(id);

        ProductSaveDTO productSaveDTO = new ProductSaveDTO();

        BeanUtils.copyProperties(product , productSaveDTO);
        productSaveDTO.setDetailsImageUrls(productDetails.getImageUrls());

        productSaveDTO.setProductSkuList(productSkus);

        /*
            前端复用方法时可以额外提交一个操作类型参数  此处可以根据操作类型判断是否要查询品牌分类名称
         */
        Brand brand = brandMapper.selectById(product.getBrandId());
        Category category1 = categoryMapper.selectById(product.getCategory1Id());
        Category category2 = categoryMapper.selectById(product.getCategory2Id());
        Category category3 = categoryMapper.selectById(product.getCategory3Id());

        productSaveDTO.getParams().put("brandName" , brand.getName());
        productSaveDTO.getParams().put("category1Name" , category1.getName());
        productSaveDTO.getParams().put("category2Name" , category2.getName());
        productSaveDTO.getParams().put("category3Name" , category3.getName());
        return productSaveDTO;

    }
    @Transactional(rollbackFor = Exception.class)
    //当更新商品数据时，清除首页缓存的数据
    @CacheEvict(value = "spzx:index" , key = "'hotData'")
    @Override
    public void updateById(ProductSaveDTO productSaveDTO) {
        //1、更新spu
        Product product = new Product();
        BeanUtils.copyProperties(productSaveDTO , product);
        productMapper.updateById(product);

        //2、更新productDetails
        ProductDetails productDetails = new ProductDetails();
        productDetails.setId(productSaveDTO.getId());
        productDetails.setImageUrls(productSaveDTO.getDetailsImageUrls());

        productDetailsMapper.updateById(productDetails);
        //3、更新productSku列表

        productSaveDTO.getProductSkuList().forEach(productSku -> {
            productSku.setSkuName(product.getName()+" "+ productSku.getSkuSpec());
            productSkuMapper.updateById(productSku);
        });



    }

    @Override
    public void updateStatus(Long id, Integer status) {
        productMapper.updateStatusById(id,status);
        productSkuMapper.updateStatusByProductId(id,status);

    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        productMapper.updateAuditStatusById(id,auditStatus);
    }
}
